@extends('admin.layout.layout', ['row' => @$page])

@section('contain')

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions">
            </div>
        </div>

        <div>
            <div>
                <div class="row">
                    <div class="col-3 text-right pt-1">
                        <strong>{{ ts('jsmc') }}：</strong>
                    </div>
                    <div class="col-6">
                        <input type="text" readonly value="{{ @$data->name }}" class="form-control">
                    </div>
                </div>
                <div class="row my-3">
                    <div class="col-3 text-right pt-1">
                        <strong>{{ ts('qxgl') }}：</strong>
                    </div>
                    <div class="col-6">
                        <table class="table table-s2">
                            <tbody>
                            @foreach(@$permission as $v)
                                @if(@$v['checked'])
                                    <tr>
                                        <td>{{ @$v['label'] }}</td>
                                    </tr>
                                 @endif
                                @empty($v['children'])
                                @else
                                    @foreach(@$v['children'] as $c)
                                        @if(@$c['checked'])
                                            <tr>
                                                <td class="pl-5">{{ @$c['label'] }}</td>
                                            </tr>
                                        @endif
                                    @endforeach
                                @endif
                            @endforeach
                            {{--@foreach(@$permission as $v)
                            <tr class="{{ @$v['checked'] ? 'text-primary' : 'text-secondary' }}">
                                <td>{{ @$v['label'] }}</td>
                            </tr>
                            @empty($v['children'])
                            @else
                                @foreach(@$v['children'] as $c)
                                    <tr class="{{ @$c['checked'] ? 'text-primary' : 'text-secondary' }}">
                                        <td class="pl-5">{{ @$c['label'] }}</td>
                                    </tr>
                                @endforeach
                            @endif
                            @endforeach--}}
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-3"></div>
                    <div class="col">
                        @if(@$data->id > 3)
                            <a href="/{{ request()->path() }}/edit" class="btn btn-primary">{{ ts('xg') }}</a>
                        @endif
                    </div>
                </div>
            </div>
            @if(@$data->id > 3)

                <h3 class="section_sub_title">{{ ts('yhlb') }}
                    <button class="btn btn-danger ml-5" onclick="addNew(0)">{{ ts('addnew') }}</button>
                </h3>
                <table class="table table-s2 text-center">
                    <thead>
                    <tr>
                        <th>{{ ts('ZH') }}</th>
                        <th>{{ ts('lxr') }}</th>
                        <th>{{ ts('dqzt') }}</th>
                        <th>{{ ts('cz') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach(\App\Models\Admin::where('user_type', @$data->id)->get(['id', 'unique_id', 'status', 'first_name']) as $v)
                        <tr>
                            <td>
                                {{ @$v->unique_id }}
                            </td>
                            <td>
                                {{ @$v->first_name }}
                            </td>
                            <td>
                                {{ ts('ust'.@$v->status) }}
                            </td>
                            <td>
                                <span class="btn btn-danger btn-sm" onclick="editStatus('{{ $v->id }}', '{{ $v->status }}', '{{ $v->unique_id }}')">{{ $v->status != 1 ? ts('active') : ts('DEACTIVE') }}</span>
                                <span class="btn btn-warning btn-sm" onclick="edit('{{ $v->id }}')">{{ ts('xg') }}</span>
                                <span class="btn btn-danger btn-sm" onclick="resetPassword('{{ $v->id }}', '{{ $v->unique_id }}')">{{ ts('CZMM') }}</span>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            @endif

            <div class="modal fade" id="RolesUserModal">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title font-green">{{ ts('addnew') }}</h5>
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                        </div>
                        <div class="modal-body">
                            <form>
                                <input type="text" hidden name="id">
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('ZH') }}</div>
                                    <div class="form-input">
                                        <input type="text" readonly name="unique_id" class="form-control">
                                        <div class="text">账号长度不小于6位</div>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('PASSWORD') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="password" class="form-control">
                                        <div class="text">{{ ts('MMCDBDXY') }}</div>
                                    </div>
                                </div>
                                <div class="form-item">
                                    <div class="form-label" required="">{{ ts('LXR') }}</div>
                                    <div class="form-input">
                                        <input type="text" name="first_name" class="form-control">
                                    </div>
                                </div>
                            </form>

                            <div class="py-3 text-center">
                                <button type="button" class="btn btn-primary" onclick="handleSubmit()">{{ ts('confirm') }}</button>
                                <button type="button" class="btn btn-secondary ml-4" onclick="closeModal()">{{ ts('CANCEL') }}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>


    </div>


@endsection

@section('script')
    <script>
        function editStatus(userid, status, title) {
            var form = {
                t: 'editStatus',
                id: userid,
                bid: '{{ @$data->id }}',
                status: status,
            };
            var msg;
            if (status != 1) {
                msg = '确定启用 ' + title + ' ？'
            }  else {
                msg = '确定停用 ' + title + ' ？'
            }
            mySwal(msg)
                .then(function (d) {
                    request.post('/admin/ajax/roles', form)
                        .then(function (d) {
                            alert(d.msg || '修改成功！');
                            window.location.reload();
                        })
                        .catch(function (d) {
                            alert(d.msg || '修改失败！');
                            window.location.reload();
                        })
                })
                .catch(function (d) {
                    console.log(d)
                });

        }

        function resetPassword(id, unique_id) {
            var form = {
                t: 'resetPassword',
                id: id,
                bid: '{{ @$data->id }}',
                status: status,
            };
            var url = '/admin/ajax/roles';
            mySwal('确定对账号 ' + unique_id + ' 重置密码？')
                .then(function (d) {
                    request.post(url, form)
                        .then(function (d) {
                            mySwal2(d.msg)
                        })
                        .catch(function (d) {
                            console.log(d)
                        })
                })
                .catch(function (d) {
                    console.log(d)
                })
        }

        function resetForm() {
            $('#RolesUserModal form')[0].reset();
            $('#RolesUserModal form input[name="unique_id"]').attr('readonly', true)
        }
        function closeModal() {
            $('#RolesUserModal').modal('hide')
        }
        function handleSubmit() {
            var msg, state=true;
            var form = formObj($('#RolesUserModal form'));
            if (!form.unique_id) {
                msg = '账号不能为空';
            }
            if (form.unique_id.length < 6 && !msg) {
                msg = '账号长度不小于6位'
            }
            if (!form.id && !form.password && !msg) {
                msg = '密码不能为空'
            }
            if (!form.id && form.password.length < 6 && !msg) {
                msg = '密码长度不小于6位'
            }
            if (!form.first_name && !msg) {
                msg = '联系人不能为空'
            }
            if (msg) {
                alert(msg);
                return false;
            }
            form = Object.assign({}, form, {
                t: 'addOrUpdate',
                bid: '{{ @$data->id }}',
            });
            request.post('/admin/ajax/roles', form)
                .then(function (d) {
                    alert(d.msg || '增加成功！');
                    window.location.reload();
                    console.log(d);
                })
                .catch(function (d) {
                    console.log(d);
                })
        }
        function addNew () {
            resetForm();
            $('#RolesUserModal .modal-title').text(ts('addnew'));
            $('#RolesUserModal input[name="password"]').parents('.form-item').show();
            $('#RolesUserModal form input[name="unique_id"]').attr('readonly', false);
            $('#RolesUserModal').modal()
        }

        function edit (id) {
            resetForm();
            var form = {
                t: 'getUserInfo',
                id: id,
                bid: '{{ @$data->id }}',
            };
            request.post('/admin/ajax/roles', form)
                .then(function (d) {
                    var data = d.data;
                    $('#RolesUserModal .modal-title').text(ts('xg'));
                    $('#RolesUserModal input[name="id"]').val(data.id);
                    $('#RolesUserModal input[name="unique_id"]').val(data.unique_id).attr('readonly', true);
                    $('#RolesUserModal input[name="first_name"]').val(data.first_name);
                    $('#RolesUserModal input[name="password"]').parents('.form-item').hide();

                    $('#RolesUserModal').modal()
                })
                .catch(function (d) {
                    alert(d.msg || '无操作权限');
                })
        }
        $(document).ready(function () {
            $('.table.bt-table').bootstrapTable({}).removeClass('d-none')
        })
    </script>
@endsection